<?php

namespace app\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Bew extends Base{

    public function menuinfo(){
        $lists = Db::table('admin_sys_menu')->where('parent_id',0)->order('sort desc,menu_id')->select();
        View::assign(['lists'=>$lists]);
        return View::fetch();
    }

    public function menuadd(){
        if(Request::isPost()){
            $label = input('post.label');
            $sort = intval(input('post.sort'));
            $status = intval(input('post.status'));
            if(empty($label)){
                return_json(1,'请输入导航名');
            }
            $insert = Db::table('admin_sys_menu')->insert([
                'label'=>$label,
                'sort'=>$sort,
                'status'=>$status
            ]);
            if(empty($insert)){
                return_json(1,'添加失败,请重试');
            }
            return_json(0,'添加成功');
        }else{
            return View::fetch();
        }
    }

    public function menuedit(){
        if(Request::isPost()){
            $menu_id = input('post.menu_id');
            $label = input('post.label');
            $sort = intval(input('post.sort'));
            $status = intval(input('post.status'));
            if(empty($label)){
                return_json(1,'请输入导航名');
            }
            $insert = Db::table('admin_sys_menu')->where('menu_id',$menu_id)->update([
                'label'=>$label,
                'sort'=>$sort,
                'status'=>$status
            ]);
            if(empty($insert)){
                return_json(1,'修改失败,请重试');
            }
            return_json(0,'修改成功');
        }else{
            $menu_id = input('get.menu_id');
            if(empty($menu_id)){
                $this->error('请传入导航ID');
            }
            $menu = Db::table('admin_sys_menu')->where('menu_id',$menu_id)->find();
            View::assign([
                'menu'=>$menu
            ]);
            return View::fetch();
        }
    }

    public function menudel(){
        $menu_id = input('get.menu_id');
        if(empty($menu_id)){
            return_json(1,'删除失败,请稍后再试');
        }

        $del = Db::table('admin_sys_menu')->where('menu_id',$menu_id)->delete();

        if(empty($del)){
            return_json(1,'删除失败,请稍后再试');
        }else{
            return_json(0,'删除成功');
        }
    }

    public function buttonInfo(){
        $menu_id = input('get.menu_id');
        $lists = Db::table('admin_sys_menu')->where('parent_id',$menu_id)->select()->toArray();
        View::assign([
            'lists' => $lists
        ]);
        return View::fetch();
    }

}